//
//  TableViewCell.swift
//  iModern
//
//  Created by Ralf on 15/6/24.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit

class TableViewCell: UITableViewCell
{
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let screen      = UIScreen.mainScreen().bounds

    override func awakeFromNib()
    {
        super.awakeFromNib()
        self.accessoryType = UITableViewCellAccessoryType.None;
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func pushViewController(controller: UIViewController!)
    {
        if appDelegate.navigationController?.respondsToSelector("interactivePopGestureRecognizer:") != nil
        {
            appDelegate.navigationController?.interactivePopGestureRecognizer!.enabled = true
            appDelegate.navigationController?.interactivePopGestureRecognizer!.delegate = nil
        }

        appDelegate.navigationController?.pushViewController(controller, animated: true)
    }

    func login()
    {
        let controller = LoginViewController()
        pushViewController(controller)
    }

    @IBAction func popViewController(sender: AnyObject)
    {
        appDelegate.navigationController?.popViewControllerAnimated(true)
    }

    func alertNetworkConnectionException()
    {
        let alertView = UIAlertView(title: "提示", message: "网络连接异常，请检查你的网络设置", delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertUserStatusException()
    {
        let alertView = UIAlertView(title: "提示", message: "您的帐号可能已经在另一台设备上登录", delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertErrorMessage(message: String!)
    {
        let alertView = UIAlertView(title: "提示", message: message, delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertWarningMessage(message: String!)
    {
        let alertView = UIAlertView(title: "提示", message: message, delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func alertSuccessMessage(message: String!)
    {
        let alertView = UIAlertView(title: "提示", message: message, delegate: nil, cancelButtonTitle: "确定")
        alertView.show()
    }

    func verifyResultset(rs: ResultSet?) -> Bool
    {
        if rs == nil
        {
            alertNetworkConnectionException()
            return false
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)

                appDelegate.navigationController?.popToRootViewControllerAnimated(true)
                return false
            }

            alertErrorMessage(rs!.errorMessage)
            return false
        }

        return true
    }

}
